package com.sskj.xgja.optional;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MVPFragment;
import com.sskj.xgja.MyApplication;
import com.sskj.xgja.R;
import com.sskj.xgja.main.MainActivity;
import com.sskj.xgja.market.MarketDetailActivity;
import com.sskj.xgja.market.MarketSearchListActivity;
import com.sskj.xgja.market.bean.MarketData;
import com.sskj.xgja.util.TipUtilLogin;

import java.util.List;

import butterknife.BindView;

/**
 * 自选
 */
public class OptionalFragment extends MVPFragment<OptionalPresenter> {

    private int page = 1;
    private int pageSize = 10;
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recordsView)
    RecyclerView recordsView;
    @BindView(R.id.edit_coin_name)
    EditText editCoinName;
    @BindView(R.id.market_search)
    ImageView marketSearch;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private BaseAdapter<MarketData> adapter;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_optional;
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        recordsView.setLayoutManager(new LinearLayoutManager(getContext()));
        recordsView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth(2)
                .setDividerColor(getResources().getColor(R.color.color_eeeeee))
        );
        adapter = new BaseAdapter<MarketData>(R.layout.optional_item, null, recordsView, true) {
            @Override
            public void bind(ViewHolder holder, MarketData item) {
                if (item == null) {
                    return;
                }
                holder.getView(R.id.coin_delete).setVisibility(toolbar.getRightButton().getText().equals("编辑") ? View.GONE : View.VISIBLE);
                holder.getView(R.id.coin_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.removeOptional(item.getCode(), true);
                    }
                });
                holder.setText(R.id.tv_cn_name, item.getName())
                        .setText(R.id.tv_en_name, item.getCode())
                        .setText(R.id.tv_newest_price, item.getPrice())
                        .setText(R.id.tv_up_down, item.getChangeRate());
                if (Float.parseFloat(item.getChange()) > 0) {
                    holder.setText(R.id.tv_up_down, "+" + item.getChangeRate());
                    holder.setTextColor(R.id.tv_newest_price, color(R.color.red));
                    holder.setBackgroundRes(R.id.tv_up_down, R.drawable.market_red_bg);
                } else {
                    holder.setBackgroundRes(R.id.tv_up_down, R.drawable.market_green_bg);
                    holder.setTextColor(R.id.tv_newest_price, color(R.color.green));
                }
                ClickUtil.click(holder.getView(R.id.market_item), view -> {
                    MainActivity activity = (MainActivity) getActivity();
                    if (activity != null) {
                        activity.currentStock = item;
                    }
                    MarketDetailActivity.start(getContext(), item);
                });
            }
        };
        ClickUtil.click(marketSearch, view -> {
            if (!TextUtils.isEmpty(MyApplication.getAccount()) && !TextUtils.isEmpty(MyApplication.getToken())) {
                if (TextUtils.isEmpty(getText(editCoinName))) {
                    ToastUtils.show("请输入股票名称或代码");
                    return;
                }
                MarketSearchListActivity.start(getContext(), true, 0, getText(editCoinName));
            } else {
                TipUtilLogin.loginDialog(getContext());
            }
        });
    }

    @Override
    public void initData() {
        toolbar.setRightButtonOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter.getData().size() == 0) {
                    ToastUtils.show("暂无数据编辑");
                    return;
                }
                if (toolbar.getRightButton().getText().equals("完成")) {
                    toolbar.setRightButtonText("编辑");
                    adapter.setNewData(adapter.getData());
                } else {
                    toolbar.setRightButtonText("完成");
                    adapter.setNewData(adapter.getData());
                }
            }
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getMarketList(true);
            }
        });
        mPresenter.getMarketList(true);
    }


    @Override
    public OptionalPresenter getPresenter() {
        return new OptionalPresenter();
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    public void setData(List<MarketData> data) {
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!TextUtils.isEmpty(MyApplication.getAccount()) && !TextUtils.isEmpty(MyApplication.getToken())) {
                mPresenter.getMarketList(false);
            }
        }
    }

    public static OptionalFragment newInstance() {
        Bundle args = new Bundle();
        OptionalFragment fragment = new OptionalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getMarketList(false);
    }
}
